package org.fast.project.web.advice;

import static org.fast.project.common.consts.GlobalConst.REQUEST_ID;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import java.util.UUID;

import javax.annotation.Nonnull;

/**
 * @program: spring-web-archetype-advice
 * @description: 请求拦截器 对请求进行一些处理
 * @author: lilith·bristol
 * @date 2020-04-22 18:16
 **/

@Component
@Slf4j
public class GlobalRequestInterceptor implements WebRequestInterceptor {


    @Override
    public void preHandle(WebRequest request) {
        var requestIdObj = request.getAttribute(REQUEST_ID, WebRequest.SCOPE_REQUEST);
        if (null == requestIdObj) {
            String requestId = UUID.randomUUID().toString();
            request.setAttribute(REQUEST_ID, requestId, WebRequest.SCOPE_REQUEST);
        }
    }

    @Override
    public void postHandle(@Nonnull WebRequest request, ModelMap model) {

    }

    @Override
    public void afterCompletion(@Nonnull WebRequest request, Exception ex) {

    }
}
